import cv2
import matplotlib.pyplot as plt
import numpy as np
from holoviews.core.traversal import hierarchical
from numba import uint8
from sympy.abc import epsilon

def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

img = cv2.imread('C:/Users/nic/Desktop/opencv/picture/duo.jpg')
img_2 = img.copy()
#先把图像转化为灰度图
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#再把灰度图转换为二值图
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
#contours是轮廓信息，hierarchy是结构层次
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
#contours保存所有的轮廓信息，但不能一次计算所有的轮廓，要选择一条轮廓进行计算
cnt = contours[4]
#外接矩形
x,y,w,h = cv2.boundingRect(cnt)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv_show('img',img)

#外接圆
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
img_2 = cv2.circle(img_2,center,radius,(0,255,0),2)
cv_show('img_2',img_2)



